package utils;


import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import constant.Constant;
import entry.*;

import shape.*;


import java.io.File;
import java.io.IOException;
import java.util.*;

public class Imposition {
    public static void main(String[] args) {
//        String pdfPath = "C:\\Users\\Administrator\\Desktop\\新建文件夹\\HD125@ 80g 双胶纸 2000.pdf";
//        String outPath = "C:\\Users\\Administrator\\Desktop\\新建文件夹\\ok_北大青鸟80G双胶纸【单联骑马钉16-16】【正反印】(2000张)《584x434》《HD125》.pdf";
//        String pinbanfangshi = "双面印刷";
//        float space = 4;
//        float jiaoxian = 3;
//        float jiaoxianjubian = 2;
//
//
//        Info info = new Info();
//        info.setPath(pdfPath);
//        info.setSpace(4);
//        info.setJx(jiaoxian);
//        info.setJj(jiaoxianjubian);
//        info.setCol(4);
//        info.setRow(2);
//        info.setLianshu(1);
//        info.setPinbanfangshi(Constant.yin_shua_fang_shi_zi_fan);
//        info.setOutPath("C:\\Users\\Administrator\\Desktop\\新建文件夹\\北大青鸟80G双胶纸【单联骑马钉16-16】【正反印】(2000张)《584x434》《HD125》.pdf");
//        qimading(info);


    }


    public static Map<String, Object> qimading(Info info) {
        String pdfPath = info.getPath();
        System.out.println("打印info:"+info);
        String tempPath = "info/temp.pdf";
        float space = info.getSpace();
        float jx = info.getJx();
        float jj = info.getJj();
        int col = info.getCol();
        int row = info.getRow();
        int lian = info.getLianshu();
        String yinshuafangshi = info.getPinbanfangshi();

        String pinbanfangshi = "骑马钉" + col + "x" + row;
        Constant.singlePath = pdfPath;
        Constant.pdfReaderMap = new HashMap<Integer, PdfReader>();
        PdfReader reader = null;
        try {
            reader = new PdfReader(pdfPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Constant.pdfReaderMap.put(100, reader);


        Rectangle trim = reader.getBoxSize(1, "trim");
        float w = reader.getBoxSize(1, "media").getWidth() / Constant.pt;
        float h = reader.getBoxSize(1, "media").getHeight() / Constant.pt;
        float h1 = reader.getBoxSize(1, "media").getHeight() / Constant.pt;
        if (trim != null) {
            w = reader.getBoxSize(1, "trim").getWidth() / Constant.pt;
            h = reader.getBoxSize(1, "trim").getHeight() / Constant.pt;
            h1 = reader.getBoxSize(1, "media").getHeight() / Constant.pt;
        }
        float realChuXue = (h1 - h) / 2;
        float bb = (jj + jx) * 2;
        float pw = bb + w * col + space * (col / 2 - 1);
        float ph = bb + h * row + space * (row - 1);

        int pageCount = reader.getNumberOfPages();

        List<List<Shape>> lists = new ArrayList<List<Shape>>();


        Map<String, Object> map = new HashMap<>();

        /*
        if (yinshuafangshi.equals(Constant.yin_shua_fang_shi_zheng_fan)) {
            if (pageCount % (col * row * 2 / lian) != 0) {
                map.put("code", 201);
                map.put("message", "骑马钉" + col + "x" + row + "页数必须整除" + col * row * 2 / lian);

                ItextUtil.closeReaderMap();
                return map;
            }
        } else {
            if (pageCount % (col * row / lian) != 0&&false) {
                map.put("code", 201);
                map.put("message", "骑马钉" + col + "x" + row + "页数必须整除" + col * row * 2 / lian);

                ItextUtil.closeReaderMap();
                return map;
            }
        }
*/
        if (space == 0) {
            map.put("code", 201);
            map.put("message", "骑马钉间距不能为0");
            ItextUtil.closeReaderMap();
            return map;
        }


        List<List<P>> listp = QiMaDingUtil.getLists(pageCount, lian, col, row, yinshuafangshi,w>h);

        int m = 0;
        for (List<P> ps : listp) {
            System.out.println(ps);
            List<Shape> listTemp = new ArrayList<>();
            getJiaoXian(jx, jj, space, w, h, pw, ph, row, col, listTemp, pinbanfangshi);
            int k = 0;
            m++;
            boolean flag = false;
            if (m % 2 == 1) {
                flag = true;
            }
            for (int r = row - 1; r >= 0; r--) {
                for (int c = 0; c < col; c++) {
                    P p = ps.get(k);
                    float tempspace = 0;
                    if (c != 0) {
                        tempspace = (int) (1 + c / 2.1f) * space;
                    }
                    int index = ItextUtil.getIndex(c, r, col, row, flag, p.getRotate());
                    int pos = 0;
                    if (flag) {
                        if (c == 0) {
                            pos = 1;
                        } else if (c == col - 1) {
                            pos = 4;
                        } else if (c%2==0) {
                            pos = 2;
                        } else if (c%2==1) {
                            pos = 3;
                        }
                    } else {
                        if (c == 0) {
                            pos = 1;
                        } else if (c == col - 1) {
                            pos = 4;
                        } else if (c%2==0) {
                            pos = 2;
                        } else if (c%2==1) {
                            pos = 3;
                        }
                    }


                    listTemp.add(new QiMaDingShape(new Position(c * (space + w) - tempspace, r * (space + h)), p.getPage(), p.getRotate(), w, h, index, realChuXue, space, jj, jx, pos));
                    k++;
                }

            }
            lists.add(listTemp);
        }


        ItextUtil.createByPaper(lists, tempPath, new Size(pw, ph));
//        new File(tempPath).renameTo(new File(info.getOutPath()));
        ItextUtil.mergePdf(tempPath, info.getOutPath(), pinbanfangshi,info);
        map.put("code", 200);
        map.put("message", "成功");




        ItextUtil.closeReaderMap();
        return map;
    }

    public static Map<String, Object> jiaozhuang(Info info,int pageCount2) {
        String pdfPath = info.getPath();

        String tempPath = "info/temp.pdf";
        float space = info.getSpace();
        float jx = info.getJx();
        float jj = info.getJj();
        int col = info.getCol();
        int row = info.getRow();
        int lian = info.getLianshu();
        String yinshuafangshi = info.getPinbanfangshi();

        String pinbanfangshi = "胶装" + col + "x" + row;
        Constant.singlePath = pdfPath;
        Constant.pdfReaderMap = new HashMap<Integer, PdfReader>();
        PdfReader reader = null;
        try {
            reader = new PdfReader(pdfPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Constant.pdfReaderMap.put(100, reader);


        Rectangle trim = reader.getBoxSize(1, "trim");
        float w = reader.getBoxSize(1, "media").getWidth() / Constant.pt;
        float h = reader.getBoxSize(1, "media").getHeight() / Constant.pt;
        float h1 = reader.getBoxSize(1, "media").getHeight() / Constant.pt;
        if (trim != null) {
            w = reader.getBoxSize(1, "trim").getWidth() / Constant.pt;
            h = reader.getBoxSize(1, "trim").getHeight() / Constant.pt;
            h1 = reader.getBoxSize(1, "media").getHeight() / Constant.pt;
        }
        float realChuXue = (h1 - h) / 2;
        float bb = (jj + jx) * 2;
        float pw = bb + w * col + space * (col - 1);
        float ph = bb + h * row + space * (row - 1);

        int pageCount = reader.getNumberOfPages();

        if (info.getIndex() == 1) {
            pw = (jj + jx) * 2 + w * col + space * (col / 2 - 1) + col / 2.0f * (pageCount2- 4) / 2.0f * 0.1f;
            ph = (jj + jx) * 2 + h * row + space * (row - 1);
        }


        List<List<Shape>> lists = new ArrayList<List<Shape>>();


        Map<String, Object> map = new HashMap<>();

        if (space == 0) {
            map.put("code", 201);
            map.put("message", "胶装间距不能为0");
            ItextUtil.closeReaderMap();
            return map;
        }


        List<List<P>> listp = JiaoZhuangUtil.getLists(pageCount, lian, col, row, yinshuafangshi);
        if(listp==null){
            ItextUtil.closeReaderMap();
            new File(pdfPath).delete();
            return null;
        }
        int m = 0;
        float keyv = (pageCount2- 4) / 2.0f * 0.1f;
        for (List<P> ps : listp) {
            System.out.println(ps);
            List<Shape> listTemp = new ArrayList<>();
            getJiaoXianjiaozhuang(jx, jj, space, w, h, pw, ph, row, col, listTemp, pinbanfangshi,info.getIndex(),pageCount2);
            int k = 0;
            m++;
            boolean flag = false;
            if (m % 2 == 1) {
                flag = true;
            }
            for (int r = row - 1; r >= 0; r--) {
                for (int c = 0; c < col; c++) {
                    P p = ps.get(k);
                    float tempspace = 0;
                    float v = 0;
                    if (c != 0) {
                        tempspace = (int) (1 + c / 2.1f) * space;
                        v = (int) (1 + c / 2.1f) * keyv;
                    }
                    int index = ItextUtil.getIndex(c, r, col, row, flag, p.getRotate());
                    int pos = 0;
                    if (true) {
                        if (c == 0) {
                            pos = 1;
                        } else if (c == col - 1) {
                            pos = 4;
                        } else if (c == 1) {
                            pos = 2;
                        } else if (c == 2) {
                            pos = 3;
                        }
                    } else {
                        if (c == 0) {
                            pos = 4;
                        } else if (c == col - 1) {
                            pos = 1;
                        } else if (c == 1) {
                            pos = 3;
                        } else if (c == 2) {
                            pos = 2;
                        }
                    }

                    if (info.getIndex() == 1) {
                        if (c % 2 == 1) {
                            listTemp.add(new JiaoZhuangShape(new Position(c * (space + w) - tempspace +v, r * (space + h)), p.getPage(), p.getRotate(), w, h, index, realChuXue, space, jj, jx,  info.getIndex(),keyv,pos));

                        } else {
                            listTemp.add(new JiaoZhuangShape(new Position(c * (space + w) - tempspace+v, r * (space + h)), p.getPage(), p.getRotate(), w, h, index, realChuXue, space, jj, jx,  info.getIndex(),keyv,pos));

                        }

                    } else {
                        listTemp.add(new JiaoZhuangShape(new Position(c * (space + w), r * (space + h)), p.getPage(), p.getRotate(), w, h, index, realChuXue, space, jj, jx,  info.getIndex(),keyv,pos));

                    }
                    k++;
                }

            }
            lists.add(listTemp);
        }


        ItextUtil.createByPaper(lists, tempPath, new Size(pw, ph));
//        new File(tempPath).renameTo(new File(info.getOutPath()));
        ItextUtil.mergePdf(tempPath, info.getOutPath(), pinbanfangshi,info);
        map.put("code", 200);
        map.put("message", "成功");




        ItextUtil.closeReaderMap();
        return map;
    }

    public static void getJiaoXian(float jx, float jj, float sp, float w, float h, float pw, float ph, int row, int col, List<Shape> list, String pinbanfangshi) {

        float jxw = Constant.jiao_xian_cu_xi;
        PdfSpotColor color = Constant.reg;

        //四个角落的脚线
        list.add(new LineShape(ItextUtil.getJXPositions(jx, 0, jx, jx), jxw, color));
        list.add(new LineShape(ItextUtil.getJXPositions(pw - jx, 0, pw - jx, jx), jxw, color));
        list.add(new LineShape(ItextUtil.getJXPositions(jx, ph, jx, ph - jx), jxw, color));
        list.add(new LineShape(ItextUtil.getJXPositions(pw - jx, ph, pw - jx, ph - jx), jxw, color));

        list.add(new LineShape(ItextUtil.getJXPositions(0, jx, jx, jx), jxw, color));
        list.add(new LineShape(ItextUtil.getJXPositions(0, ph - jx, jx, ph - jx), jxw, color));
        list.add(new LineShape(ItextUtil.getJXPositions(pw, jx, pw - jx, jx), jxw, color));
        list.add(new LineShape(ItextUtil.getJXPositions(pw, ph - jx, pw - jx, ph - jx), jxw, color));

        if (pinbanfangshi.indexOf("骑马钉") != -1) {
            float tempspace = 0;
            for (int i = 0; i < col; i++) {
                if (i != 0) {
                    tempspace = (int) (1 + i / 2.1f) * sp;
                }
                list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) - tempspace, 0, jx + jj + i * (w + sp) - tempspace, jx), jxw, color));
                if (sp != 0 || i == col - 1)
                    list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) + w - tempspace, 0, jx + jj + i * (w + sp) + w - tempspace, jx), jxw, color));
                list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) - tempspace, ph, jx + jj + i * (w + sp) - tempspace, ph - jx), jxw, color));
                if (sp != 0 || i == col - 1)
                    list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) + w - tempspace, ph, jx + jj + i * (w + sp) + w - tempspace, ph - jx), jxw, color));
            }
        } else {
            for (int i = 0; i < col; i++) {
                list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp), 0, jx + jj + i * (w + sp), jx), jxw, color));
                if (sp != 0 || i == col - 1)
                    list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) + w, 0, jx + jj + i * (w + sp) + w, jx), jxw, color));
                list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp), ph, jx + jj + i * (w + sp), ph - jx), jxw, color));
                if (sp != 0 || i == col - 1)
                    list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) + w, ph, jx + jj + i * (w + sp) + w, ph - jx), jxw, color));
            }
        }
        for (int i = 0; i < row; i++) {
            list.add(new LineShape(ItextUtil.getJXPositions(0, jx + jj + i * (h + sp), jx, jx + jj + i * (h + sp)), jxw, color));
            if (sp != 0 || i == row - 1)
                list.add(new LineShape(ItextUtil.getJXPositions(0, jx + jj + i * (h + sp) + h, jx, jx + jj + i * (h + sp) + h), jxw, color));
            list.add(new LineShape(ItextUtil.getJXPositions(pw, jx + jj + i * (h + sp), pw - jx, jx + jj + i * (h + sp)), jxw, color));
            if (sp != 0 || i == row - 1)
                list.add(new LineShape(ItextUtil.getJXPositions(pw, jx + jj + i * (h + sp) + h, pw - jx, jx + jj + i * (h + sp) + h), jxw, color));
        }
    }

    public static void getJiaoXianjiaozhuang(float jx, float jj, float sp, float w, float h, float pw, float ph, int row, int col, List<Shape> list, String pinbanfangshi,int index,int pageCount) {

        float jxw = Constant.jiao_xian_cu_xi;
        PdfSpotColor color = Constant.reg;

        //四个角落的脚线
        list.add(new LineShape(ItextUtil.getJXPositions(jx, 0, jx, jx), jxw, color));
        list.add(new LineShape(ItextUtil.getJXPositions(pw - jx, 0, pw - jx, jx), jxw, color));
        list.add(new LineShape(ItextUtil.getJXPositions(jx, ph, jx, ph - jx), jxw, color));
        list.add(new LineShape(ItextUtil.getJXPositions(pw - jx, ph, pw - jx, ph - jx), jxw, color));

        list.add(new LineShape(ItextUtil.getJXPositions(0, jx, jx, jx), jxw, color));
        list.add(new LineShape(ItextUtil.getJXPositions(0, ph - jx, jx, ph - jx), jxw, color));
        list.add(new LineShape(ItextUtil.getJXPositions(pw, jx, pw - jx, jx), jxw, color));
        list.add(new LineShape(ItextUtil.getJXPositions(pw, ph - jx, pw - jx, ph - jx), jxw, color));

        //    if (c % 2 == 1) {
        //
        //                            listTemp.add(new JiaoZhuangShape(new Position(c * (space + w) - tempspace + v, r * (space + h)), p.getPage(), p.getRotate(), w, h, index, realChuXue, space, jj, jx, pos, info.getIndex()));
        //                            v++;
        //                        } else {
        //                            listTemp.add(new JiaoZhuangShape(new Position(c * (space + w) - tempspace, r * (space + h)), p.getPage(), p.getRotate(), w, h, index, realChuXue, space, jj, jx, pos, info.getIndex()));
        //
        //                        }
        float v2 = (pageCount- 4) / 2.0f * 0.1f;

        if (pinbanfangshi.indexOf("胶装") != -1) {
            if(index==1){

                float tempspace = 0;
                float v = 0;
                for (int i = 0; i < col; i++) {
                    if (i != 0) {
                        tempspace = (int) (1 + i / 2.1f) * sp;
                        v = (int) (1 + i / 2.1f) * v2;
                    }
                    if(i%2==1){
                        list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) - tempspace+v, 0, jx + jj + i * (w + sp) - tempspace+v, jx), jxw, color));
                        if (sp != 0 || i == col - 1)
                            list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) + w - tempspace+v, 0, jx + jj + i * (w + sp) + w - tempspace+v, jx), jxw, color));
                        list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) - tempspace+v, ph, jx + jj + i * (w + sp) - tempspace+v, ph - jx), jxw, color));
                        if (sp != 0 || i == col - 1)
                            list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) + w - tempspace+v, ph, jx + jj + i * (w + sp) + w - tempspace+v, ph - jx), jxw, color));
                    }else{
                        list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) - tempspace+v, 0, jx + jj + i * (w + sp) - tempspace+v, jx), jxw, color));
                        if (sp != 0 || i == col - 1)
                            list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) + w - tempspace+v, 0, jx + jj + i * (w + sp) + w - tempspace+v, jx), jxw, color));
                        list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) - tempspace+v, ph, jx + jj + i * (w + sp) - tempspace+v, ph - jx), jxw, color));
                        if (sp != 0 || i == col - 1)
                            list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) + w - tempspace+v, ph, jx + jj + i * (w + sp) + w - tempspace+v, ph - jx), jxw, color));
                    }

                }
            }else{
                for (int i = 0; i < col; i++) {
                    list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp), 0, jx + jj + i * (w + sp), jx), jxw, color));
                    if (sp != 0 || i == col - 1)
                        list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) + w, 0, jx + jj + i * (w + sp) + w, jx), jxw, color));
                    list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp), ph, jx + jj + i * (w + sp), ph - jx), jxw, color));
                    if (sp != 0 || i == col - 1)
                        list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) + w, ph, jx + jj + i * (w + sp) + w, ph - jx), jxw, color));
                }
            }

        } else {
            for (int i = 0; i < col; i++) {
                list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp), 0, jx + jj + i * (w + sp), jx), jxw, color));
                if (sp != 0 || i == col - 1)
                    list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) + w, 0, jx + jj + i * (w + sp) + w, jx), jxw, color));
                list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp), ph, jx + jj + i * (w + sp), ph - jx), jxw, color));
                if (sp != 0 || i == col - 1)
                    list.add(new LineShape(ItextUtil.getJXPositions(jx + jj + i * (w + sp) + w, ph, jx + jj + i * (w + sp) + w, ph - jx), jxw, color));
            }
        }
        for (int i = 0; i < row; i++) {
            list.add(new LineShape(ItextUtil.getJXPositions(0, jx + jj + i * (h + sp), jx, jx + jj + i * (h + sp)), jxw, color));
            if (sp != 0 || i == row - 1)
                list.add(new LineShape(ItextUtil.getJXPositions(0, jx + jj + i * (h + sp) + h, jx, jx + jj + i * (h + sp) + h), jxw, color));
            list.add(new LineShape(ItextUtil.getJXPositions(pw, jx + jj + i * (h + sp), pw - jx, jx + jj + i * (h + sp)), jxw, color));
            if (sp != 0 || i == row - 1)
                list.add(new LineShape(ItextUtil.getJXPositions(pw, jx + jj + i * (h + sp) + h, pw - jx, jx + jj + i * (h + sp) + h), jxw, color));
        }
    }
}
